package com.e3.bill.client;

import java.util.List;

import com.e3.web.core.client.GridListener;
import com.e3.web.core.client.GwtAsyncCallback;
import com.e3.web.core.model.SuperModel;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public abstract class BillListPanel<D extends SuperModel,M extends SuperModel> extends LayoutContainer {
	
	private BillPagingGridPanel<D> masterGrid;
	
	private BillGridPanel<M> slaveGrid;
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setLayout(new FitLayout());
		
		ContentPanel contentPanel = new ContentPanel(new BorderLayout());
		initContentPanel(contentPanel);
		
//		ToolBar toobar = new ToolBar();
//		initToolBar(toobar);
//		contentPanel.setTopComponent(toobar);
		add(contentPanel);
	}

	protected void initToolBar(ToolBar toobar) {
		
	}

	protected void initContentPanel(ContentPanel contentPanel) {
		contentPanel.setHeaderVisible(false);
		BorderLayoutData bld = new BorderLayoutData(LayoutRegion.NORTH);
		bld.setSplit(true);
		contentPanel.add(getMasterGrid(),bld);
		
		bld = new BorderLayoutData(LayoutRegion.CENTER);
		contentPanel.add(getSlaveGrid(),bld);
	}

	public BillPagingGridPanel<D> getMasterGrid() {
		if(masterGrid==null){
			masterGrid = createMasterGrid();
			masterGrid.addGridListener(new GridListener<D>() {

				public void onGridSelectionChanged(D model) {
					onMasterGridSelectionChanged(model);
				}

				public void onGridRowDoubClicked(D model) {
					onMasterGridRowDoubleClicked(model);
				}
				
			});
		}
		return masterGrid;
	}
	
	protected void onMasterGridSelectionChanged(D model){
		if(model == null){
			return;
		}
		executeQuerySlaves(model,getSlaveModelsAsyncCallback(model));
	}


	protected void onMasterGridRowDoubleClicked(D model){
		
	}
	
	public BillGridPanel<M> getSlaveGrid() {
		if(slaveGrid==null){
			slaveGrid = createSlaveGrid();
			slaveGrid.addGridListener(new GridListener<M>() {

				public void onGridSelectionChanged(M model) {
					onSlaveGridSelectionChanged(model);
				}

				public void onGridRowDoubClicked(M model) {
					onSlaveGridRowDoubleClicked(model);
				}
			});
		}
		return slaveGrid;
	}
	
	protected void onSlaveGridSelectionChanged(M model){
		
	}
	
	protected void onSlaveGridRowDoubleClicked(M model){
		
	}

	protected void setMasterGridContextMenu(Menu menu){
		if(menu!=null){
			masterGrid.setContextMenu(menu);
		}
	}
	
	protected void setSlaveGridContextMenu(Menu menu){
		if(menu!=null){
			slaveGrid.setContextMenu(menu);
		}
	}
	
	protected AsyncCallback<List<M>> getSlaveModelsAsyncCallback(final D model) {
		return new GwtAsyncCallback<List<M>>(this) {
			@Override
			public void doSuccess(List<M> result) {
				getSlaveGrid().setGridValues(result);
			}
		};
	}
	
	protected abstract BillPagingGridPanel<D> createMasterGrid();
	
	protected abstract BillGridPanel<M> createSlaveGrid();
	
	protected abstract void executeQuerySlaves(D model,AsyncCallback<List<M>> slaveModelsAsyncCallback) ;
	
}
